package com.example.makerspace.utils;

import io.jsonwebtoken.*;
import org.junit.Test;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
public class JwtUtils {


    private static final long ONEMONTH = 1000L * 60  * 60 * 24;

    private  static  final  long DESTROYED=1000*3;

    private static final String SIGNATURE = "MAKERSPACE_SIGNATURE";

    public static String createToken(Integer id, String username, String role){
        JwtBuilder jwtBuilder = Jwts.builder();
           return jwtBuilder
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .claim("username", username)
                .claim("role", role)
                .claim("id",id)
                // 有效期1天
                .setExpiration(new Date(System.currentTimeMillis() + ONEMONTH))
                .setId(id.toString())
                .signWith(SignatureAlgorithm.HS256, SIGNATURE)
                .compact();
    }
    public static String destroyedToken(Integer id, String username, String role){
        JwtBuilder jwtBuilder = Jwts.builder();
        return jwtBuilder
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //payload
                .claim("username", username)
                .claim("role", role)
                .claim("id",id)
                // 有效期1天
                .setExpiration(new Date(System.currentTimeMillis() + DESTROYED))
                .setId(id.toString())
                .signWith(SignatureAlgorithm.HS256, SIGNATURE)
                .compact();
    }

    public static boolean checkToken(String token){
        JwtParser jwtParser = Jwts.parser();
        try {
            Jws<Claims> claimsJws = jwtParser.setSigningKey(SIGNATURE).parseClaimsJws(token);
            System.out.println(claimsJws);
            Claims body = claimsJws.getBody();
            Date expiration = body.getExpiration();
            System.out.println(expiration);
        }catch (Exception exception){
            return false;
        }
        return true;
    }

    public static Claims parseToken(String token){
        JwtParser jwtParser = Jwts.parser();
        Jws<Claims> claimsJws = jwtParser.setSigningKey(SIGNATURE).parseClaimsJws(token);
        return claimsJws.getBody();
    }

    public static String getUsernameByToken(String token){
        Claims claims = parseToken(token);
        return(String)claims.get("username");
    }

    public static String getRole(HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = parseToken(token);
        return(String)claims.get("role");
    }

    public static String getUsername(HttpServletRequest request) {
        String token = request.getHeader("token");
        if(token==null||token.equals("")){
            return  "用户未登录";
        }

        Claims claims = parseToken(token);
        return(String)claims.get("username");
    }



    public static String getUserID(HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims claims = parseToken(token);
        return(String)claims.get("id");
    }
    @Test
    public void test(){
        String jwtToken = createToken(1, "294704837@qq.com", "admin");
        JwtParser jwtParser = Jwts.parser();
        Jws<Claims> claimsJws = jwtParser.setSigningKey(SIGNATURE).parseClaimsJws(jwtToken);
        Claims body = claimsJws.getBody();
        System.out.println(body.get("username"));
        System.out.println(body.get("role"));
        System.out.println(body.getId());
        System.out.println(body.getExpiration());
    }
}
